#include "showkeyController.h"
#include "global/global.h"

ShowkeyController::ShowkeyController(const QRect &rect, QWidget* parent = nullptr)
    : m_screenkeyTimer(nullptr),
      m_lineShowTimer(nullptr),
      m_showkeyWindow(nullptr)
{
    if(m_info.isWlcom()){
        showkeyRect = rect;
        m_dbus_effect = new QDBusInterface("com.kylin.Wlcom",
                                             "/com/kylin/Wlcom/Effect",
                                             "com.kylin.Wlcom.Effect",
                                             QDBusConnection::sessionBus());
        m_dbus_showkey = new QDBusInterface("com.kylin.Wlcom",
                                             "/com/kylin/Wlcom/Showkey",
                                             "com.kylin.Wlcom.Showkey",
                                             QDBusConnection::sessionBus());

        if (!m_dbus_effect->isValid()) {
            qDebug() << "Wlcom-DBus-effect 连接失败：" << m_dbus_effect->lastError().message();
        }


        if (!m_dbus_showkey->isValid()) {
            qDebug() << "Wlcom-DBus-showkey 连接失败：" << m_dbus_showkey->lastError().message();
        }
    }

    m_showkeyWindow = new ShowkeyWindow(rect, parent);


    if (!(m_info.waylandDectected() && (m_info.windowManager() == DesktopInfo::UKUI)))
    {
        pThread = new QThread();
        xev = new ShowkeyGetkey();

        xev->moveToThread( pThread );
        connect( xev, SIGNAL( pressedKey( QString ) ), this, SLOT( showScreenkeyWindow( QString ) ) );
        connect( xev, SIGNAL( finished() ), this, SLOT( finished() ) );
    }

    m_screenkeyTimer = new QTimer( this );
    connect( m_screenkeyTimer, SIGNAL( timeout() ), this, SLOT( hideScreenkeyWindow() ) );

    m_lineShowTimer = new QTimer( this );
    connect( m_lineShowTimer, SIGNAL(timeout()), this, SLOT(refreshLineShow()));
    m_lineShowTimer->start(1500);

}

ShowkeyController::~ShowkeyController()
{
}

bool ShowkeyController::eventFilter(QObject *obj, QEvent *e)
{
//    if (e->type() == QEvent::KeyPress)
//    {
//        if (m_info.waylandDectected() && (m_info.windowManager() == DesktopInfo::UKUI))
//        {
//            if (Global::isRecordKey)
//            {
//                QString sKeyName = "";
//                QKeyEvent *pKeyEvent = static_cast<QKeyEvent*>(e);
//                if (pKeyEvent->key() == Qt::Key_Shift)
//                {
//                    sKeyName = "Shift";
//                }
//                else if (pKeyEvent->key() == Qt::Key_Control)
//                {
//                    sKeyName = "Ctrl";
//                }
//                else if (pKeyEvent->key() == Qt::Key_CapsLock)
//                {
//                    sKeyName = "Caps";
//                }
//                else
//                {
//                    QKeySequence keySequence(pKeyEvent->key());
//                    sKeyName = keySequence.toString(QKeySequence::PortableText);;
//                }

//                showScreenkeyWindow(sKeyName);
//            }
//        }
//    }

    return QObject::eventFilter(obj, e);
}

void ShowkeyController::finished()
{
    if (!(m_info.waylandDectected() && (m_info.windowManager() == DesktopInfo::UKUI)))
    {
        if (m_bChecked)
        {
            xev->start();
        }
    }
}

void ShowkeyController::showkeyReadKey( bool checked  )
{
    m_bChecked = checked;

    if(m_info.isWlcom()){

        QDBusMessage reply = m_dbus_effect->call("EnableEffect", "showkey", checked);
        if (reply.type() != QDBusMessage::ReplyMessage) {
            qDebug() << "EnableEffect 调用失败：" << reply.errorMessage();
        }

        if(checked && !showkeyRect.isEmpty()){
            QDBusMessage reply_showkey = m_dbus_showkey->call("createShowkey",
                                                              quint32(showkeyRect.x()),
                                                              quint32(showkeyRect.y()),
                                                              quint32(showkeyRect.width()),
                                                              quint32(showkeyRect.height()));
            if (reply_showkey.type() != QDBusMessage::ReplyMessage) {
                qDebug() << "createShowkey 调用失败：" << reply_showkey.errorMessage();
            }
        }
    }

    if (!(m_info.waylandDectected() && (m_info.windowManager() == DesktopInfo::UKUI)))
    {
        if ( checked )
        {
            xev->start();
        }
        else
        {
            xev->terminate();
            hideScreenkeyWindow();
        }
    }
}

void ShowkeyController::showScreenkeyWindow( QString value)
{
    m_screenkeyTimer->stop();
    m_screenkeyTimer->start(3000);
    m_showkeyWindow->show();
    m_showkeyWindow->addText(value);
}

void ShowkeyController::refreshLineShow()
{
    m_showkeyWindow->clearShow();
}

void ShowkeyController::hideScreenkeyWindow()
{
    m_showkeyWindow->hide();
    m_screenkeyTimer->stop();
}
